package gui.panel_new;

import enums.*;
import gui.other.SylogismViewerDialog;
import j_util.Utiles;
import misc.BundlesProvider;
import model.Polisilogismo;
import model.Premisa;
import model.Sylogism;
import model.Venn;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Destinado a la ensenanza de los diagramas de Venn;
 * @author Jose Martin Rozanec;
 */
public class VennPanel extends OpenSavePanel {
    private javax.swing.JButton analizarButton;
    private javax.swing.JComboBox combo1;
    private javax.swing.JComboBox combo2;
    private javax.swing.JComboBox combo3;
    private javax.swing.JTextField conclusionField;
    private javax.swing.JLabel conclusionLabel;
    private javax.swing.JLabel convertirEnLabel;
    private javax.swing.JPanel jPanel1;
    private BasicVennPanel userVennPanel;
    private BasicVennPanel solutionVennPanel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField premisaMayorField;
    private javax.swing.JLabel premisaMayorLabel;
    private javax.swing.JTextField premisaMenorField;
    private javax.swing.JLabel premisaMenorLabel;
    private javax.swing.JButton resetearButton;
    private javax.swing.JLabel terminoMayorLabel;
    private javax.swing.JLabel terminoMedioLabel;
    private javax.swing.JLabel terminoMenorLabel;

    private JButton cross1Button;
    private JButton cross2Button;
    private JButton cross3Button;
    private JButton cross4Button;
    private JButton cross5Button;
    private JButton cross6Button;
    private JButton cross7Button;
    private JButton fill1Button;
    private JButton fill2Button;
    private JButton fill3Button;
    private JButton fill4Button;
    private JButton fill5Button;
    private JButton fill6Button;
    private JButton fill7Button;

    private JPanel panel1;
    private JPanel panel2;

    private Sylogism sylogism;
    private int premisaLabelsStringLength;

    private ResourceBundle textBundle;

    private VennEnum[] vennEnums;

    /**
     * Constructor;
     *
     * @param saveButton;
     * @param openButton;
     * @param fileChooser;
     */
    public VennPanel(JButton saveButton, JButton openButton, JFileChooser fileChooser) {
        this.saveButton = saveButton;
        this.openButton = openButton;
        this.fileChooser = fileChooser;
        this.sylogism = new Sylogism();
        premisaLabelsStringLength = 55;
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        fileFilter = FileFilterEnum.XML;
        initComponents();
        reset();
    }

    /**
     * Inicializa los componentes;
     */
    private void initComponents() {
        setBackground(Color.WHITE);
        jPanel1 = new javax.swing.JPanel();
        premisaMayorField = new javax.swing.JTextField();
        premisaMayorLabel = new javax.swing.JLabel();
        convertirEnLabel = new javax.swing.JLabel();
        combo1 = new javax.swing.JComboBox();
        premisaMenorLabel = new javax.swing.JLabel();
        premisaMenorField = new javax.swing.JTextField();
        combo2 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        conclusionLabel = new javax.swing.JLabel();
        conclusionField = new javax.swing.JTextField();
        combo3 = new javax.swing.JComboBox();
        analizarButton = new javax.swing.JButton();
        resetearButton = new javax.swing.JButton();
        userVennPanel = new BasicVennPanel();
        solutionVennPanel = new BasicVennPanel();
        terminoMayorLabel = new javax.swing.JLabel();
        terminoMedioLabel = new javax.swing.JLabel();
        terminoMenorLabel = new javax.swing.JLabel();

        cross1Button = new JButton();
        cross2Button = new JButton();
        cross3Button = new JButton();
        cross4Button = new JButton();
        cross5Button = new JButton();
        cross6Button = new JButton();
        cross7Button = new JButton();
        fill1Button = new JButton();
        fill2Button = new JButton();
        fill3Button = new JButton();
        fill4Button = new JButton();
        fill5Button = new JButton();
        fill6Button = new JButton();
        fill7Button = new JButton();


        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.SILOGISMO.toString())));

        premisaMayorLabel.setText(Utiles.getFormattedString(
                textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
        premisaMayorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        convertirEnLabel.setText(textBundle.getString(TextBundleEnum.CONVERTIR_EN.toString()));

        combo1.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.FIRST, combo1.getSelectedIndex());
            }
        });

        premisaMenorLabel.setText(
                Utiles.getFormattedString(textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
        premisaMenorLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));

        combo2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.FIRST, combo2.getSelectedIndex());
            }
        });

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        conclusionLabel.setText(
                Utiles.getFormattedString(textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
        conclusionLabel.setHorizontalTextPosition(SwingConstants.LEFT);
        conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));

        combo3.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"A", "E", "I", "O"}));
        combo3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboConvertOrComplete(PremisaTypeEnum.CONCLUSION, combo3.getSelectedIndex());
            }
        });

        analizarButton.setText(textBundle.getString(TextBundleEnum.ANALIZAR.toString()));
        analizarButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                analize();
            }
        });
        analizarButton.setMnemonic(KeyEvent.VK_A);

        resetearButton.setText(textBundle.getString(TextBundleEnum.RESET.toString()));
        resetearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetearButton.setMnemonic(KeyEvent.VK_B);

        userVennPanel.setBackground(new java.awt.Color(255, 255, 255));

        solutionVennPanel.setBackground(new java.awt.Color(255, 255, 255));

        terminoMayorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()) + ":");

        terminoMedioLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()) + ":");

        terminoMenorLabel.setText(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()) + ":");

        premisaMayorField.setEditable(false);
        premisaMenorField.setEditable(false);
        conclusionField.setEditable(false);


        premisaMayorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(premisaMayorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMayorField.getText()), PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(0, 0, 0));
                    premisaMayorLabel.setText(
                            Utiles.getFormattedString(textBundle.getString(
                                    TextBundleEnum.PREMISA_MAYOR.toString()), "  -  " +
                                    textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                                    sylogism.getPremisa(PremisaTypeEnum.FIRST).getTermsExtensionString()));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.FIRST);
                    premisaMayorField.setForeground(new Color(255, 0, 0));
                    premisaMayorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MAYOR.toString()), ""));
                    premisaMayorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
                }
            }
        });


        premisaMenorField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(premisaMenorField.getText())) {
                    sylogism.setPremisa(new Premisa(premisaMenorField.getText()), PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(0, 0, 0));
                    premisaMenorLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.PREMISA_MENOR.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.SECOND).getTermsExtensionString()));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.SECOND);
                    premisaMenorField.setForeground(new Color(255, 0, 0));
                    premisaMenorLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.PREMISA_MENOR.toString()), ""));
                    premisaMenorLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.PREMISA_LARGO)));
                }
            }
        });

        conclusionField.addCaretListener(new CaretListener() {
            public void caretUpdate(CaretEvent e) {
                if (Premisa.isComplete(conclusionField.getText())) {
                    sylogism.setPremisa(new Premisa(conclusionField.getText()), PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(0, 0, 0));
                    conclusionLabel.setText(Utiles.getFormattedString(textBundle.getString(
                            TextBundleEnum.CONCLUSION.toString()), "  -  " +
                            textBundle.getString(TextBundleEnum.EXTENSION_PARTES.toString()) + ": " +
                            sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).getTermsExtensionString()));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_BREVE)));
                } else {
                    sylogism.setPremisa(null, PremisaTypeEnum.CONCLUSION);
                    conclusionField.setForeground(new Color(255, 0, 0));
                    conclusionLabel.setText(Utiles.getFormattedString(
                            textBundle.getString(TextBundleEnum.CONCLUSION.toString()), ""));
                    conclusionLabel.setIcon(Utiles.createImageIcon(URLEnum.getReference(URLEnum.CONCLUSION_LARGO)));
                }
            }
        });

        cross1Button.setText("1");
        cross1Button.setMnemonic('1');
        cross1Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross1Button.setBackground(new Color(51, 255, 153));
                fill1Button.setBackground(new Color(240, 240, 240));
                vennEnums[0] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross2Button.setText("2");
        cross2Button.setMnemonic('2');
        cross2Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross2Button.setBackground(new Color(51, 255, 153));
                fill2Button.setBackground(new Color(240, 240, 240));
                vennEnums[1] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross3Button.setText("3");
        cross3Button.setMnemonic('3');
        cross3Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross3Button.setBackground(new Color(51, 255, 153));
                fill3Button.setBackground(new Color(240, 240, 240));
                vennEnums[2] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross4Button.setText("4");
        cross4Button.setMnemonic('4');
        cross4Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross4Button.setBackground(new Color(51, 255, 153));
                fill4Button.setBackground(new Color(240, 240, 240));
                vennEnums[3] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross5Button.setText("5");
        cross5Button.setMnemonic('5');
        cross5Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross5Button.setBackground(new Color(51, 255, 153));
                fill5Button.setBackground(new Color(240, 240, 240));
                vennEnums[4] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross6Button.setText("6");
        cross6Button.setMnemonic('6');
        cross6Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross6Button.setBackground(new Color(51, 255, 153));
                fill6Button.setBackground(new Color(240, 240, 240));
                vennEnums[5] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        cross7Button.setText("7");
        cross7Button.setMnemonic('7');
        cross7Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cross7Button.setBackground(new Color(51, 255, 153));
                fill7Button.setBackground(new Color(240, 240, 240));
                vennEnums[6] = VennEnum.CROSS;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill1Button.setText("1");
        fill1Button.setMnemonic('q');
        fill1Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill1Button.setBackground(new Color(51, 255, 153));
                cross1Button.setBackground(new Color(240, 240, 240));
                vennEnums[0] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill2Button.setText("2");
        fill2Button.setMnemonic('w');
        fill2Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill2Button.setBackground(new Color(51, 255, 153));
                cross2Button.setBackground(new Color(240, 240, 240));
                vennEnums[1] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill3Button.setText("3");
        fill3Button.setMnemonic('e');
        fill3Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill3Button.setBackground(new Color(51, 255, 153));
                cross3Button.setBackground(new Color(240, 240, 240));
                vennEnums[2] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill4Button.setText("4");
        fill4Button.setMnemonic('r');
        fill4Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill4Button.setBackground(new Color(51, 255, 153));
                cross4Button.setBackground(new Color(240, 240, 240));
                vennEnums[3] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill5Button.setText("5");
        fill5Button.setMnemonic('t');
        fill5Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill5Button.setBackground(new Color(51, 255, 153));
                cross5Button.setBackground(new Color(240, 240, 240));
                vennEnums[4] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill6Button.setText("6");
        fill6Button.setMnemonic('y');
        fill6Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill6Button.setBackground(new Color(51, 255, 153));
                cross6Button.setBackground(new Color(240, 240, 240));
                vennEnums[5] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });

        fill7Button.setText("7");
        fill7Button.setMnemonic('u');
        fill7Button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fill7Button.setBackground(new Color(51, 255, 153));
                cross7Button.setBackground(new Color(240, 240, 240));
                vennEnums[6] = VennEnum.FILL;
                userVennPanel.setVennEnums(vennEnums);
            }
        });
        sizeTextFields();
        layoutComponents();
    }

    /**
     * Hace el layout de los componentes visuales;
     */
    private void layoutComponents() {
        setBackground(Color.WHITE);
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        int x = 0;
        int y = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        jPanel1.setLayout(new GridBagLayout());

        /*      PREMISAS     */
        jPanel1.add(premisaMayorLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(convertirEnLabel, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 6);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(premisaMayorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(combo1, gridBagConstraints);

        gridBagConstraints.gridx = (x -= 6);
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorLabel, gridBagConstraints);

        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(premisaMenorField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(combo2, gridBagConstraints);

        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.gridx = (x -= 6);
        gridBagConstraints.gridy = ++y;
        jSeparator1.setPreferredSize(new Dimension(550, 2));
        jPanel1.add(jSeparator1, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridy = ++y;
        jPanel1.add(conclusionLabel, gridBagConstraints);

        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 5;
        jPanel1.add(conclusionField, gridBagConstraints);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = (x += 6);
        jPanel1.add(combo3, gridBagConstraints);

        jPanel1.setMinimumSize(new Dimension(655, 250));
        jPanel1.setPreferredSize(new Dimension(655, 250));
        x = 0;
        y = 0;
        /*      END OF PREMISAS     */

        /*      BUTTONS     */
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        add(jPanel1, gridBagConstraints);

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 7));
        buttonsPanel.setBackground(Color.WHITE);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(analizarButton);
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(new JLabel());
        buttonsPanel.add(resetearButton);
        buttonsPanel.add(new JLabel());

        gridBagConstraints.gridy = 1;
        add(buttonsPanel, gridBagConstraints);
        /*      END OF BUTTONS     */

        userVennPanel.setMinimumSize(new Dimension(230, 230));
        userVennPanel.setPreferredSize(new Dimension(230, 230));
        panel1 = new JPanel();
        panel1.setBackground(new java.awt.Color(255, 255, 255));
        panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.DIAGRAMA_RESOLVER.toString())));
        panel1.add(userVennPanel);

        gridBagConstraints.gridwidth = 1;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(5, 40, 5, 30);
        add(panel1, gridBagConstraints);

        panel2 = new JPanel();
        panel2.setBackground(new java.awt.Color(255, 255, 255));
        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.DIAGRAMA_SOLUCION.toString())));
        panel2.add(solutionVennPanel);
        solutionVennPanel.setMinimumSize(new Dimension(230, 230));
        solutionVennPanel.setPreferredSize(new Dimension(230, 230));
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new Insets(5, 30, 5, 30);
        add(panel2, gridBagConstraints);

        JPanel controls = new JPanel(new GridLayout(2, 8));
        controls.setBackground(Color.WHITE);
        controls.add(new JLabel(textBundle.getString(TextBundleEnum.CRUZ.toString())));
        controls.add(cross1Button);
        controls.add(cross2Button);
        controls.add(cross3Button);
        controls.add(cross4Button);
        controls.add(cross5Button);
        controls.add(cross6Button);
        controls.add(cross7Button);

        controls.add(new JLabel(textBundle.getString(TextBundleEnum.SOMBREADO.toString())));
        controls.add(fill1Button);
        controls.add(fill2Button);
        controls.add(fill3Button);
        controls.add(fill4Button);
        controls.add(fill5Button);
        controls.add(fill6Button);
        controls.add(fill7Button);

        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        add(controls, gridBagConstraints);
    }

    /**
     * Establece el tamano de los textFields;
     */
    private void sizeTextFields() {
        premisaMayorField.setColumns(50);
        premisaMayorField.setMinimumSize(new Dimension(600, 20));
        premisaMenorField.setColumns(50);
        premisaMenorField.setMinimumSize(new Dimension(600, 20));
        conclusionField.setColumns(50);
        conclusionField.setMinimumSize(new Dimension(600, 20));
    }

    /**
     * Resetea el diagrama de Venn;
     */
    private void reset() {
        vennEnums = new VennEnum[7];
        for (int j = 0; j < vennEnums.length; j++) {
            vennEnums[j] = VennEnum.NOTHING;
        }
        if (sylogism != null) {
            setSylogismTerms();
        } else {
            solutionVennPanel.setTerminoMayor(textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString()));
            solutionVennPanel.setTerminoMedio(textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString()));
            solutionVennPanel.setTerminoMenor(textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString()));
        }
        
        userVennPanel.setVennEnums(vennEnums);
        solutionVennPanel.setVennEnums(vennEnums);
        cross1Button.setBackground(new Color(240, 240, 240));
        cross2Button.setBackground(new Color(240, 240, 240));
        cross3Button.setBackground(new Color(240, 240, 240));
        cross4Button.setBackground(new Color(240, 240, 240));
        cross5Button.setBackground(new Color(240, 240, 240));
        cross6Button.setBackground(new Color(240, 240, 240));
        cross7Button.setBackground(new Color(240, 240, 240));
        fill1Button.setBackground(new Color(240, 240, 240));
        fill2Button.setBackground(new Color(240, 240, 240));
        fill3Button.setBackground(new Color(240, 240, 240));
        fill4Button.setBackground(new Color(240, 240, 240));
        fill5Button.setBackground(new Color(240, 240, 240));
        fill6Button.setBackground(new Color(240, 240, 240));
        fill7Button.setBackground(new Color(240, 240, 240));

        panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.DIAGRAMA_RESOLVER.toString())));
        panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                new java.awt.Color(204, 204, 204), 1, true),
                textBundle.getString(TextBundleEnum.DIAGRAMA_SOLUCION.toString())));
    }

    /**
     * Convierte la premisa a la extension solicitada;
     *
     * @param premisaTypeEnum;
     * @param selectedIndex;
     */
    public void comboConvertOrComplete(PremisaTypeEnum premisaTypeEnum, int selectedIndex) {
        if (sylogism.getPremisa(premisaTypeEnum) != null) {
            switch (selectedIndex) {
                case 0://A
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.A);
                    break;
                case 1://E
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.E);
                    break;
                case 2://I
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.I);
                    break;
                case 3://O
                    sylogism.getPremisa(premisaTypeEnum).convert(ExtensionEnum.O);
                    break;
            }
            switch (premisaTypeEnum) {
                case FIRST:
                    premisaMayorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case SECOND:
                    premisaMenorField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
                case CONCLUSION:
                    conclusionField.setText(sylogism.getPremisa(premisaTypeEnum).toString());
                    break;
            }
        }
    }

    /**
     * Setea el silogismo;
     *
     * @param sylogism;
     */
    public void setSylogism(Sylogism sylogism) {
        this.sylogism = sylogism;
        if (sylogism.getPremisa(PremisaTypeEnum.FIRST) != null) {
            premisaMayorField.setText(sylogism.getPremisa(PremisaTypeEnum.FIRST).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.SECOND) != null) {
            premisaMenorField.setText(sylogism.getPremisa(PremisaTypeEnum.SECOND).toString());
        }
        if (sylogism.getPremisa(PremisaTypeEnum.CONCLUSION) != null) {
            conclusionField.setText(sylogism.getPremisa(PremisaTypeEnum.CONCLUSION).toString());
        }
        setSylogismTerms();

    }

    /**
     * Realiza el analisis del silogismo en un diagrama de Venn;
     */
    private void analize() {
        VennEnum[] solution = Venn.analizeSylogism(sylogism);
        solutionVennPanel.setVennEnums(solution);
        if (Venn.isCorrect(vennEnums, sylogism)) {
            panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                    new javax.swing.border.LineBorder(new java.awt.Color(10, 190, 10), 2, true),
                    textBundle.getString(TextBundleEnum.DIAGRAMA_RESOLVER.toString())));
            panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                    new java.awt.Color(10, 190, 10), 2, true),
                    textBundle.getString(TextBundleEnum.DIAGRAMA_SOLUCION.toString())));
        } else {
            panel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                    new javax.swing.border.LineBorder(new java.awt.Color(210, 30, 30), 2, true),
                    textBundle.getString(TextBundleEnum.DIAGRAMA_RESOLVER.toString())));
            panel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(
                    new java.awt.Color(210, 30, 30), 2, true),
                    textBundle.getString(TextBundleEnum.DIAGRAMA_SOLUCION.toString())));
        }
    }

    /**
     * Setea los terminos del silogismo sobre el diagrama de Venn;
     */
    private void setSylogismTerms() {
        String terminoMayor = sylogism.getTerminoMayor();
        String terminoMedio = sylogism.getTerminoMedio();
        String terminoMenor = sylogism.getTerminoMenor();
        if (terminoMayor == null || terminoMayor.equals("")) {
            terminoMayor = textBundle.getString(TextBundleEnum.TERMINO_MAYOR.toString());
        }
        if (terminoMedio == null || terminoMedio.equals("")) {
            terminoMedio = textBundle.getString(TextBundleEnum.TERMINO_MEDIO.toString());
        }
        if (terminoMenor == null || terminoMenor.equals("")) {
            terminoMenor = textBundle.getString(TextBundleEnum.TERMINO_MENOR.toString());
        }

        userVennPanel.setTerminoMayor(terminoMayor);
        userVennPanel.setTerminoMedio(terminoMedio);
        userVennPanel.setTerminoMenor(terminoMenor);
        userVennPanel.repaint();

        solutionVennPanel.setTerminoMayor(terminoMayor);
        solutionVennPanel.setTerminoMedio(terminoMedio);
        solutionVennPanel.setTerminoMenor(terminoMenor);
        solutionVennPanel.repaint();
    }

    protected void setSaveActionListener() {
        class SaveSylogismAction extends AbstractAction {
            public SaveSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.SAVE_AS)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = fileChooser.showSaveDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    Utiles.saveTextFileAlertingOverwrite(path, ".xml", Venn.toXML(sylogism, vennEnums));
                }
            }
        }
        saveButton.setAction(new SaveSylogismAction());
    }

    protected void setSaveButtonEnabled() {
        saveButton.setEnabled(true);
    }

    protected void setOpenActionListener() {
        class OpenSylogismAction extends AbstractAction {
            public OpenSylogismAction() {
                super("", Utiles.createImageIcon(URLEnum.getReference(URLEnum.OPEN)));
            }

            public void actionPerformed(ActionEvent e) {
                String path = "";
                int state = fileChooser.showOpenDialog(JOptionPane.getRootFrame());
                if (state == JFileChooser.APPROVE_OPTION) {
                    path = fileChooser.getSelectedFile().getPath();
                    try {
                        RandomAccessFile randomAccessFile = new RandomAccessFile(path, "r");

                        String xml = randomAccessFile.readUTF();

                        if (xml.startsWith(Sylogism.openTag())) {
                            reset();
                            setSylogism(Sylogism.parseXML(xml));
                        } else {
                            if (xml.startsWith(Polisilogismo.openTag())) {
                                SylogismViewerDialog sylogismViewerDialog =
                                        new SylogismViewerDialog(Polisilogismo.parseXML(xml).getIterator());
                                Utiles.centerDialog(sylogismViewerDialog);
                                sylogismViewerDialog.setSize(337, 160);
                                sylogismViewerDialog.setResizable(false);
                                sylogismViewerDialog.setModal(true);
                                sylogismViewerDialog.setVisible(true);
                                if (sylogismViewerDialog.accepted()) {
                                    reset();
                                    setSylogism(sylogismViewerDialog.getSylogism());
                                }
                            } else {
                                if (xml.startsWith(Venn.openTag())) {
                                    setSylogism(Venn.sylogismFromXML(xml));
                                    vennEnums = Venn.vennEnumsFromXML(xml);
                                    userVennPanel.setVennEnums(vennEnums);
                                } else {
                                    JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                                            textBundle.getString(
                                                    TextBundleEnum.WARNING_ARCHIVO_INCORRECTO_VENN.toString()));
                                }
                            }
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        openButton.setAction(new OpenSylogismAction());
    }

    protected void setOpenButtonEnabled() {
        openButton.setEnabled(true);
    }

    protected void setFocusOnComponent() {
        //remains unimplemented...
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("VennPanel");
        frame.setContentPane(new VennPanel(new JButton(), new JButton(), new JFileChooser()));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
